package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import service.DiagnosticianService;
import service.FiebreService;
import service.LugarDolorAbdominalService;
import service.TipoDolorAbdominalService;

import command.LitiasisCommand;

public class SintomaController extends SimpleFormController {

	private DiagnosticianService diagnosticianService;
	private LugarDolorAbdominalService lugarDolorAbdominalService;
	private TipoDolorAbdominalService tipoDolorAbdominalService;
	private FiebreService fiebreService;

	private LitiasisCommand litiasisCommand;

	public SintomaController() {
		this.setCommandClass(LitiasisCommand.class);
	}

	protected Map<String, Object> referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
		if (!this.isFormSubmission(request)) {			
				this.litiasisCommand.clearDiagnosticos();
		}
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("doloresAbdominales", tipoDolorAbdominalService.getAll());
		model.put("lugarDoloresAbdominales", lugarDolorAbdominalService.getAll());
		model.put("opcionesFiebre", fiebreService.getAll());
		return model;
	}

	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		response.setHeader("Cache-Control","no-cache");
		
		LitiasisCommand litiasisCommand = (LitiasisCommand) command;

		String idLugarDolor = litiasisCommand.getIdLugarDolorAbdominal();
		String idTipoDolor = litiasisCommand.getIdTipoDolorAbdominal();

		this.litiasisCommand.setLugarDolor(lugarDolorAbdominalService.getById(idLugarDolor));
		this.litiasisCommand.setDolorAbdominal(tipoDolorAbdominalService.getById(idTipoDolor));

		List<String> diagnosticos = diagnosticianService.diagnose(this.litiasisCommand, DiagnosticianService.RULES_1);
		
		HttpSession session = request.getSession(true);
		session.setAttribute("litiasisCommand", this.litiasisCommand);

		ModelAndView modelAndView = super.onSubmit(request, response, command, errors);
		modelAndView.addAllObjects(referenceData(request, command, errors));
		modelAndView.addObject("diagnosticos", diagnosticos);
		return modelAndView;
	}

	public void setDiagnosticianService(DiagnosticianService diagnosticianService) {
		this.diagnosticianService = diagnosticianService;
	}

	public void setLugarDolorAbdominalService(LugarDolorAbdominalService lugarDolorAbdominslService) {
		this.lugarDolorAbdominalService = lugarDolorAbdominslService;
	}

	public void setTipoDolorAbdominalService(TipoDolorAbdominalService tipoDolorAbdominalService) {
		this.tipoDolorAbdominalService = tipoDolorAbdominalService;
	}

	public void setLitiasisCommand(LitiasisCommand litiasisCommand) {
		this.litiasisCommand = litiasisCommand;
	}
	
	public void setFiebreService(FiebreService fiebreService) {
		this.fiebreService = fiebreService;
	}

}
